package com.lin.game.controller;

import com.lin.game.entity.RechargeOrder;
import com.lin.game.service.IRechargeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
public class RechargeOrderController {

    @Autowired
    private IRechargeOrderService rechargeOrderService;

    /**
     * 下单  欢乐豆 ： 充值金额 = 100 ： 1
     */
    @RequestMapping("/order/create")
    public String createOrder(Integer uid, double money){

        String orderid = UUID.randomUUID().toString();

        //double Integer -> BitDecimal
        BigDecimal moneyBD = BigDecimal.valueOf(money);
        BigDecimal numberBD = BigDecimal.valueOf(100);

        //创建订单对象
        RechargeOrder rechargeOrder = new RechargeOrder();
        rechargeOrder.setuId(uid);//下单用户id
        rechargeOrder.setMoney(moneyBD);//下单的金额
        rechargeOrder.setOrderId(orderid);//订单号
        rechargeOrder.setStatus(0);//未支付
        rechargeOrder.setCreateTime(new Date());//下单时间
        rechargeOrder.setBeanNumber(moneyBD.multiply(numberBD).intValue());//充值的欢乐豆数量 money * 100

        //添加订单
        rechargeOrderService.insertOrder(rechargeOrder);

        return orderid;
    }

    /**
     * 查询订单列表 根据用户id
     */
    @RequestMapping("/order/list")
    public List<RechargeOrder> queryList(Integer uid){
        return rechargeOrderService.queryRechargeOrderByUid(uid);
    }

    /**
     * 订单支付的接口
     */
    @RequestMapping("/order/pay")
    public String orderPay(String orderid){
        System.out.println("接收到支付的订单：" + orderid);

//        调用支付宝进行支付
//        AlipayClient alipayClient =  new DefaultAlipayClient(
//                "https://openapi.alipay.com/gateway.do" ,
//                APP_ID,
//                APP_PRIVATE_KEY,
//                FORMAT,
//                CHARSET,
//                ALIPAY_PUBLIC_KEY,
//                SIGN_TYPE);  //获得初始化的AlipayClient
//        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request
//        alipayRequest.setReturnUrl( "http://domain.com/CallBack/return_url.jsp" );
//        alipayRequest.setNotifyUrl( "http://domain.com/CallBack/notify_url.jsp" ); //在公共参数中设置回跳和通知地址
//        alipayRequest.setBizContent( "{"  +
//                "    \"out_trade_no\":\"20150320010101001\","  +
//                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
//                "    \"total_amount\":88.88,"  +
//                "    \"subject\":\"Iphone6 16G\","  +
//                "    \"body\":\"Iphone6 16G\","  +
//                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
//                "    \"extend_params\":{"  +
//                "    \"sys_service_provider_id\":\"2088511833207846\""  +
//                "    }" +
//                "  }" ); //填充业务参数
//        String form= "" ;
//        try  {
//            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
//        }  catch  (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        httpResponse.setContentType( "text/html;charset="  + CHARSET);
//        httpResponse.getWriter().write(form); //直接将完整的表单html输出到页面
//        httpResponse.getWriter().flush();
//        httpResponse.getWriter().close();
//

        return "支付宝的支付页面";
    }
}